package com.example.controller;

import com.example.common.Result;
import com.example.entity.MonitoringData;
import com.example.service.MonitoringDataService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/air_data")
@CrossOrigin(origins = "http://localhost:5173", allowCredentials = "true")
public class MonitoringDataController {
    private final MonitoringDataService monitoringDataService;

    public MonitoringDataController(MonitoringDataService monitoringDataService) {
        this.monitoringDataService = monitoringDataService;
    }

    @GetMapping
    public Result<List<MonitoringData>> getAllData() {
        List<MonitoringData> data = monitoringDataService.getAllData();
        return Result.success(data);
    }

    @GetMapping("/{siteId}")
    public Result<List<MonitoringData>> getDataBySiteId(@PathVariable Integer siteId) {
        List<MonitoringData> data = monitoringDataService.getDataBySiteId(siteId);
        if (data != null && !data.isEmpty()) {
            return Result.success(data);
        }
        return Result.error("未找到该站点的监测数据");
    }

    @GetMapping("/time")
    public Result<List<MonitoringData>> getDataByTime(
            @RequestParam("recorded_at") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate recordedAt) {
        List<MonitoringData> data = monitoringDataService.getDataByRecordedAt(recordedAt);
        if (data != null && !data.isEmpty()) {
            return Result.success(data);
        }
        return Result.error("未找到该时间的监测数据");
    }

    @GetMapping("/family/{familyId}")
    public Result<List<MonitoringData>> getDataByFamilyId(@PathVariable Integer familyId) {
        List<MonitoringData> data = monitoringDataService.getDataByFamilyId(familyId);
        if (data != null && !data.isEmpty()) {
            return Result.success(data);
        }
        return Result.error("未找到该家庭的监测数据");
    }

    @DeleteMapping("/{dataId}")
    public Result<Void> deleteData(@PathVariable Long dataId) {
        if (monitoringDataService.deleteData(dataId)) {
            return Result.success(null);
        }
        return Result.error("删除失败");
    }

    @PostMapping
    public Result<Void> addData(@RequestBody MonitoringData data) {
        // 验证必要参数
        if (data.getSiteId() == null) {
            return Result.error("监测站点ID不能为空");
        }
        if (data.getRecordedAt() == null) {
            data.setRecordedAt(LocalDateTime.now());
        }
        
        if (monitoringDataService.addData(data)) {
            return Result.success(null);
        }
        return Result.error("添加失败");
    }

    @PutMapping("/{dataId}")
    public Result<Void> updateData(@PathVariable Long dataId, @RequestBody MonitoringData data) {
        // 验证必要参数
        if (dataId == null) {
            return Result.error("数据ID不能为空");
        }
        
        // 设置dataId
        data.setDataId(dataId);
        
        // 获取原有数据
        MonitoringData existingData = monitoringDataService.getDataById(dataId);
        if (existingData == null) {
            return Result.error("数据不存在");
        }
        
        // 保持原有的不可修改字段
        data.setSiteId(existingData.getSiteId());
        data.setRecordedAt(existingData.getRecordedAt());
        
        if (monitoringDataService.updateData(data)) {
            return Result.success(null);
        }
        return Result.error("更新失败");
    }
} 